<?php
/**
 * Created by PhpStorm.
 * User: 吕冬
 * Date: 2017/7/24
 * Time: 18:10
 */

namespace app\admin\controller;


use think\Controller;
use think\Db;

class Guide extends Controller
{
//首页显示
    public function guide(){
        $list = Db::name('guide')->order('id desc')->paginate(5);
        $page = $list->render();
        return view("guide",array("data"=>$list,"page"=>$page));
    }
//    添加页面
    public function guide_add(){
        return view("Guide/guide_add");
    }

    //    查询
    public function guide_query(){
        $name = $_POST["key"];
        if($name==""){//判断name值是否为空
            $list = Db::name('guide')->order('id desc')->paginate(5);
            $page = $list->render();
            return view("guide",array("data"=>$list,"page"=>$page));
        }else{//不否为空进行模糊查询
            $list = Db::table("guide")->where("
                guide_name like '%{$name}'
                or guide_name like '%{$name}'
                or guide_name like '%{$name}%'
                ")->order('id desc')->paginate();  //查询 细分类的表 找到对应的类名
        }
        $page = $list->render();
        return view("guide",array("data"=>$list,"page"=>$page));
    }

//    添加方法
    public function guide_add_increase(){
        $data= $_POST ;
        $file = request()->file('head');
        $info = $file->move(ROOT_PATH . 'public/uploads');
        $data['head'] = $info->getSaveName();
        $aa = Db::table('guide')->insert($data);
        if ($aa) {
            return $this->success('添加成功','guide');
        } else {
            return $this->success('添加失败');
        }
    }
//    修改
    public function  guide_update(){
        $get= $_GET['id'];
        $list = Db::name('guide')->where('id',$get)->paginate();
        $this->assign('list', $list);
        return $this->fetch('guide_up');

    }
    public function guide_update_app(){
        $get= $_GET['id'];
        $data =$_POST;
        $file = request()->file('head');
        If(!$file){
            if(Db::table('guide')->where('id',$get)->update($data)){
                $this->success('修改成功','guide');
            }else{
                $this->error('修改失败');
            }
        } else{
            $file = request()->file('head');
            $info = $file->move(ROOT_PATH . 'public/uploads');
            $data['head'] = $info->getSaveName();
            if(Db::table('guide')->where('id',$get)->update($data)){
                $this->success('修改成功','guide');
            }else{
                $this->error('修改失败');
            }
        }
    }



//    删除
    public function guide_delete(){
        $aa=$_GET['id'];
        $request =Db::table('guide')->where('id',$aa)->delete();
        if ($request) {
            return $this->success('删除成功','guide');
        } else {
            return $this->success('删除失败');
        }
    }

}